<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="报文时间" prop="time">
        <el-date-picker
          v-model="dateRange"
          :editable="false"
          :clearable="false"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="监测站点" prop="pointId">
        <el-select v-model="queryParams.pointId" placeholder="请选择参数类型">
          <el-option label="  ——— 全部 ———  " value=""></el-option>
          <el-option
            v-for="collectPoint in collectPointList"
            :key="collectPoint.id"
            :label="collectPoint.name"
            :value="collectPoint.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="监测站点" prop="pointName">
        <el-input
          v-model="queryParams.pointName"
          placeholder="请输入监测站点名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="pointConnectLogList" @selection-change="handleSelectionChange" :max-height="tabHt">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="监测站点" align="center" prop="pointName" width="280"/>
      <el-table-column label="时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="日志" align="left" prop="connectTypes" />

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改监测站点连线日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="监测站点id" prop="pointId">
          <el-input v-model="form.pointId" placeholder="请输入监测站点id" />
        </el-form-item>
        <el-form-item label="时间" prop="time">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="类型断线/连线" prop="connectType">
          <el-select v-model="form.connectType" placeholder="请选择类型断线/连线">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPointConnectLog, getPointConnectLog, delPointConnectLog, addPointConnectLog, updatePointConnectLog, exportPointConnectLog } from "@/api/basic/pointConnectLog";
import { listCollectPoint } from "@/api/basic/collectPoint";

export default {
  name: "PointConnectLog",
  components: {
  },
  data() {
    return {
      collectPointList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监测站点连线日志表格数据
      pointConnectLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pointName: null,
        pointId: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  computed: {
    tabHt(){
      return this.$store.state.app.tableHeight.ht - 50
    }
  },
  mounted () {
    let nowDate = new Date();
    let befDate = new Date(nowDate.getTime() + 1 * 24 * 3600 * 1000);
    let byear = befDate.getFullYear();
    let bmonth = befDate.getMonth() + 1;
    let bday = befDate.getDate();
    let endTime = `${byear}-${bmonth}-${bday}`;
    // 向前推迟一周的时间点
    befDate = new Date(nowDate.getTime() - 7 * 24 * 3600 * 1000);
    byear = befDate.getFullYear();
    bmonth = befDate.getMonth() + 1;
    bday = befDate.getDate();
    let startTime = `${byear}-${bmonth}-${bday}`;
    this.dateRange = [startTime,endTime];
  },
  created() {
    this.getPointList();
    this.$nextTick(() => {
      this.getList();
    });
  },
  methods: {
    /** 查询监测站点管理列表 */
    getPointList() {
      let obj = {
        pageNum: 1,
        pageSize: 1000
      }
      listCollectPoint(obj).then(response => {
        this.collectPointList = response.rows;
      });
    },
    /** 查询监测站点连线日志列表 */
    getList() {
      this.loading = true;
      listPointConnectLog(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.pointConnectLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pointId: null,
        time: null,
        connectType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加监测站点连线日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPointConnectLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改监测站点连线日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePointConnectLog(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPointConnectLog(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除监测站点连线日志编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPointConnectLog(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有监测站点连线日志数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPointConnectLog(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
